<?php
/**
 * #Celebrate Shieh
 *             ,%%%%%%%%,
 *           ,%%/\%%%%/\%%
 *          ,%%%\c "" J/%%%
 * %.       %%%%/ o  o \%%%
 * `%%.     %%%%    _  |%%%
 *  `%%     `%%%%(__Y__)%%'
 *  //       ;%%%%`\-/%%%'
 * ((       /  `%%%%%%%'
 *  \\    .'          |
 *   \\  /       \  | |
 *    \\/         ) | |
 *     \         /_ | |__
 *     (___________))))))) 攻城湿
 *
 *        _       _
 * __   _(_)_   _(_) __ _ _ __
 * \ \ / / \ \ / / |/ _` |'_ \
 *  \ V /| |\ V /| | (_| | | | |
 *   \_/ |_| \_/ |_|\__,_|_| |_|
 */

namespace App\Services;

use App\Models\Blog\Feedback;
use App\Models\Common\Category;
use Carbon\Carbon;
use Illuminate\Http\Request;

class FeedbackService
{

    use Service;

    /**
     * 博客意见反馈首页
     *
     * @param Request $request
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function FeedbackIndexService( $request )
    {
        $select   = [
            'id', 'category_id', 'parent_id', 'user_id', 'status', 'updated_at', 'phone', 'content', 'ip',
            'country', 'area', 'region', 'city', 'county', 'fail', 'static',
        ];
        $where    = function ( $query ) use ( $request ) {
            if ( $request->filled('static') && $request->input('static') != '-1' ) $query->where('static', $request->input('static'));
            if ( $request->filled('content') ) $query->where('content', $request->input('content'));
            if ( $request->filled('category') && $request->input('category') != '-1' ) $query->where('category_id', $request->input('category'));
            $query->where('status', 1);
            $query->where('is_show', 1);
        };
        $with     = [
            'user'     => function ( $query ) {
                $query->select('id', 'name');
            },
            'category' => function ( $query ) {
                $query->select('id', 'name');
            },
            'parent'   => function ( $query ) {
                $query->select('id', 'name');
            },
            'pic'      => function ( $query ) {
                $query->select('id', 'url', 'file_url', 'filename');
            },
        ];
        $feedback = Feedback::GetModel($where, $with, $select);
        $feedback->appends([
            'content'  => $request->input('content'),
            'static'   => $request->input('static'),
            'category' => $request->input('category'),
        ])->render();
        unset($where, $with, $select);
        $select   = [ 'id', 'parent_id', 'name', 'sort_order', 'is_show', 'status' ];
        $where    = function ( $query ) use ( $request ) {
            $query->where('status', 1);
            $query->where('is_show', 1);
            $query->where('static', 2);
            $query->where('parent_id', 0);
        };
        $category = Category::GetModel($where, [], $select);
        return view('blog.feedback.index', compact('feedback', 'category', 'request'));
    }

    /**
     * 博客意见反馈删除
     *
     * @param         $id
     * @param Request $request
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function FeedbackDestroyService( $id, $request )
    {
        $ajax  = $request->ajax();
        $where = function ( $query ) use ( $id ) {
            if ( is_array($id) ) $query->whereIn('id', $id);
            else $query->where('id', $id);
        };
        Feedback::UpdateModel($where, [
            'is_show'    => 2,
            'deleted_at' => Carbon::now()->toDateTimeString(),
        ]);
        if ( $ajax ) return data(NULL, $this->getErrorCode()::message(204));
        else return back()->with('success', $this->getErrorCode()::message(204));
    }

    /**
     * 修改博客意见反馈状态
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function FeedbackStatusService( $request )
    {
        $select           = [ 'id', 'static', 'fail', ];
        $where            = function ( $query ) use ( $request ) {
            $query->where('id', $request->input('id'));
        };
        $feedback         = Feedback::FirstModel($where, [], $select);
        $feedback->static = $request->input('status', 1);
        $feedback->fail   = $request->input('fail');
        $feedback->save();
        return data(NULL, $this->getErrorCode()::message(210));
    }
}
